
package org.fpp.sqlparse;
import java.util.Arrays;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

public enum DbType {

    MYSQL(0, "mysql"),
    POSTGRESQL(1, "postgresql")
    ;

    private final int code;
    private final String desc;

    DbType(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public int getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    private static final Map<Integer, DbType> DB_TYPE_MAP =
            Arrays.stream(DbType.values()).collect(toMap(DbType::getCode,s->s));

    public static DbType of(int type) {
        if (DB_TYPE_MAP.containsKey(type)) {
            return DB_TYPE_MAP.get(type);
        }
        return null;
    }

    public static Integer count() {
        return DbType.values().length;
    }

    public static boolean sameMysql(DbType dbType){
        return MYSQL.equals(dbType);
    }

    public boolean sameMysql(){
        return sameMysql(this);
    }
    public static boolean samePostgresql(DbType dbType){
        return POSTGRESQL.equals(dbType);
    }

    public boolean samePostgresql(){
        return samePostgresql(this);
    }
}
